package com.xiaoyu.merchant.ui.activity.order;

import android.support.v4.util.TimeUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.model.OrderDetailsBean;
import com.xiaoyu.merchant.network.NetworkManager;
import com.xiaoyu.merchant.ui.fragment.order.OrderFragment;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2019/1/3
 * @Description 发货详情页面
 */
public class ShipMentsActivity extends BaseActivityWithTitle {

    @BindView(R.id.activity_examine_order_name)
    TextView mOrderNameTel;
    @BindView(R.id.activity_examine_order_address_txt)
    TextView mAddress;
    @BindView(R.id.activity_examine_order_store)
    TextView mStoreName;
    @BindView(R.id.activity_examine_order_store_img)
    ImageView mGoodsImg;
    @BindView(R.id.activity_examine_order_goods_name)
    TextView mGoodsName;
    @BindView(R.id.activity_examine_order_goods_num)
    TextView mGoodsNum;
    @BindView(R.id.activity_examine_order_goods_price)
    TextView mGoodsPrice;
    @BindView(R.id.activity_examine_ordernumber)
    TextView mOrderNumber;
    @BindView(R.id.activity_examine_orderaddtime)
    TextView mOrderTime;
    @BindView(R.id.activity_examine_paytype)
    TextView mPayType;
    @BindView(R.id.activity_examine_wares_retal)
    TextView mGoodsAllPrice;
    @BindView(R.id.activity_examine_goodsfreight)
    TextView mGoodsFreight;
    @BindView(R.id.activity_examine_order_money)
    TextView mOrderMoney;
    @BindView(R.id.choose_the_logistics)
    EditText mChooseLogistics;//选择物流
    @BindView(R.id.tracking_number)
    EditText mTrackNumber;//快递单号
    @BindView(R.id.again_tracking_number)
    EditText mAgainTrackNumber;//再次输入快递单号
    private String orderId;

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("订单详情");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_ship_ments, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        orderId = getIntent().getStringExtra(OrderFragment.ORDER_ID);
        getOrderDetails(orderId);
    }

    private void getOrderDetails(String orderid) {
        showLoading();
        NetworkManager.orderDetails(orderid, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                dissmissLoading();
                successWaitPaymentDetails(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                dissmissLoading();
            }
        });
    }

    private void successWaitPaymentDetails(String result) {
        OrderDetailsBean bean = new Gson().fromJson(result, OrderDetailsBean.class);
        mOrderNameTel.setText(bean.getData().getOrdername() + "   " + bean.getData().getTel());//名称与手机号
        mAddress.setText(bean.getData().getAddress());//地址
        mStoreName.setText(bean.getData().getGoodsstore().getStorename());//商家名称
        Glide.with(this).load(bean.getData().getDetail().getGoodsdeimg()).into(mGoodsImg);//商品图片
        mGoodsName.setText(bean.getData().getDetail().getGoodsname());//商品名称
        mGoodsNum.setText("数量：" + bean.getData().getDetail().getGoodsnumber());//商品数量
        mGoodsPrice.setText("￥ " + bean.getData().getDetail().getGoodsprice());//商品单价
        mOrderNumber.setText("订单编号：" + bean.getData().getOrdernumber());//订单编号
        mOrderTime.setText("下单时间：" + bean.getData().getOrderaddtime());//下单时间
        mPayType.setText("支付方式：" + bean.getData().getPaytype());//支付方式
        mGoodsAllPrice.setText("￥ " + bean.getData().getGrossprice());//商品总价
        int goodsFreight = Integer.parseInt(bean.getData().getGoodsfreight());
        if (goodsFreight == 0) {
            mGoodsFreight.setText("包邮");
        } else {
            mGoodsFreight.setText("+" + bean.getData().getGoodsfreight());
        }
        mOrderMoney.setText("¥" + bean.getData().getOrdermoney());//实付款
    }

    @OnClick(R.id.confirm_shipments)
    public void confirmShipClick() {
        if (TextUtils.isEmpty(mChooseLogistics.getText().toString()) || TextUtils.isEmpty(mTrackNumber.getText().toString())
                || TextUtils.isEmpty(mAgainTrackNumber.getText().toString())) {
            ToastUtil.showToast(ShipMentsActivity.this, "请输入物流与快递单号");
            return;
        }
        if (!mTrackNumber.getText().toString().equals(mAgainTrackNumber.getText().toString())) {
            ToastUtil.showToast(ShipMentsActivity.this, "两次输入的单号不同");
            return;
        }
        NetworkManager.confirmShipments(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), orderId, mAgainTrackNumber.getText().toString(),
                mChooseLogistics.getText().toString(), new CommonHttpResponser() {
                    @Override
                    public void onSuccess(String result) {
                        ToastUtil.showToast(ShipMentsActivity.this, "确认发货成功");
                        finish();
                    }

                    @Override
                    public void onFailed(String errorCode, String errorDiscription) {

                    }
                });
    }
}
